import Vue from 'vue';
import VueRouter from 'vue-router';
import home from './home';
import login from './login';
import laboratory from './laboratory';
import event from './event';
import maintain from './maintain';
import statistics from './statistics';
import info from './info';
import admin from './admin';
import Layout from '@/layout/index.vue' //主框架
const staticList = ["/login", "/register"];
Vue.use(VueRouter)
const originalPush = VueRouter.prototype.push
const originalReplace = VueRouter.prototype.replace
// push
VueRouter.prototype.push = function push (location, onResolve, onReject) {
if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
return originalPush.call(this, location).catch(err => err)
}
// replace
VueRouter.prototype.replace = function push (location, onResolve, onReject) {
if (onResolve || onReject) return originalReplace.call(this, location, onResolve, onReject)
return originalReplace.call(this, location).catch(err => err)
}

const routes = [
  login,
  {
    path: '/',
    name: 'Layout',
    component: Layout,
    redirect: '/home',
    meta: {
      cName: 'Layout'
    },
    children: [
      home,
      admin,
      ...laboratory,
      ...event,
      ...maintain,
      ...statistics,
      ...info
    ]
  }
]

const Router = new VueRouter({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes
})

/*路由拦截*/
Router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token');
  if (token) {
    if (to.path === '/login') {
      localStorage.clear()
      next();
    } else {
      next()
      // to.meta.roles.includes(role) ? next() : next(false);
    }
  } else {
    // staticList.includes(to.path) ? next() : next({
    //   path: "/login"
    // });
  }
  next()
})

export default Router;